#include "kore2.h"



/**
 * @brief ...
 *
 * ...
 *
 * @param string Filename to load
 */
kore::ResourceFile::ResourceFile(string filename)
: m_file(NULL)
{
	if (filename.length() > 0 ) 
	{
		this->status = this->openResourceFile(filename);
	}
}

/**
 * @brief ...
 *
 * ...
 */
kore::ResourceFile::~ResourceFile()
{
	if (this->m_file)
	{
		fclose(this->m_file);
	}
}

/**
 * @brief ...
 *
 * ...
 *
 * @param string Filename to load
 * @return int 0 if successful
 */
int kore::ResourceFile::openResourceFile(string filename)
{
	if (this->m_file)
	{
		printf("[resource] Flushing previous resource\n");
		fclose(this->m_file);
	}
	printf("[resource] Loading resource pool %s...\n", filename.c_str());
	this->m_file = fopen(filename.c_str(), "rb");
	if (this->m_file == NULL)
	{
		printf("[resource] Error! Failed to load resources from file: File could not be opened.\n");
		return -1;
	}

	long lSize;
	// obtain file size:
	fseek (this->m_file , 0 , SEEK_END);
	lSize = ftell (this->m_file);
	rewind (this->m_file);

	// Read the index
	kore::ResourceHeader h;
	if (fread( &h, sizeof(h), 1, this->m_file ) != 1)
	{
		printf("[resource] Error! Failed to load resources from file: Incomplete or invalid header found.\n");
		return -2;
	}

	// The file should be minimum of
	//   h.indexoffset + (sizeof(kore::ResourceIndex) * h.resourcecount)
	// bytes in size. Also, h.indexlength should really line up with
	//   sizeof(kore::ResourceIndex) * h.resourcecount)
	// or we can suspect something is fishy.
	// TODO: Sanity checking
	printf("[resource] Found %d resources (Index of 0x%lx bytes at 0x%lx)\n", h.resourcecount, h.indexlength, h.indexoffset);
	return 0;

}

kore::Resource* kore::ResourceFile::getResource(Uint32 resourceid)
{

}

kore::Resource* kore::ResourceFile::getResource(string resourcename)
{
	// Find the hash for the file and call on the other getResource.
	// If the file has got a key index, "#name" is the proper way to
	// adress it. Otherwise, it is assumed to be a path (i.e. "/data/image.png")
	// that is hashed and located in the file.
}


/**
 * @brief ...
 *
 * ...
 */
kore::ResourceManager::ResourceManager()
{
	printf("[resource] Creating manager...\n");
}

/**
 * @brief ...
 *
 * ...
 */
kore::ResourceManager::~ResourceManager()
{
	std::map<string,ResourceFile*>::iterator it;
	for( it = this->resources.begin(); it != this->resources.end(); it++ )
	{
		printf("[resource] Deleting resource %s\n", it->first.c_str());
		delete(it->second);
	}
	printf("[resource] Destroying manager...\n");
}

/**
 * @brief Loads a resource
 *
 * @param string The key to load as (alias)
 * @param string The filename to load
 * @return int 0 if successful
 */
int kore::ResourceManager::load(string key, string filename)
{
	kore::ResourceFile* file = new kore::ResourceFile(filename);
	if (file->status == 0)
	{
		this->resources[key] = file;
		printf("[resource] Added to pool as %s\n", key.c_str());
		return 0;
	}
	return -1;
}

/**
 * @brief Unloads a previously loaded resource based on its key/alias.
 *
 * @param string The key of the resource file to unload
 * @return int 0 if successful
 */
int kore::ResourceManager::unload(string key)
{
	if (this->resources.count(key) > 0)
	{
		printf("[resource] Deleting resource %s\n", key.c_str());
		delete(this->resources[key]);
		this->resources.erase(key);
	}
	else
	{
		printf("[resource] No such resource pool loaded: %s\n", key.c_str());
	}
}

kore::ResourceFile* kore::ResourceManager::getResourceFile(string key)
{
	return this->resources[key];
}